package attachment_cate

import (
	"AccessAdmin/internal/consts"
	"AccessAdmin/internal/dao"
	"AccessAdmin/internal/model"
	"AccessAdmin/internal/model/entity"
	"AccessAdmin/internal/service"
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"reflect"
)

// 附件资源
type sAttachmentCate struct {
}

func init() {
	service.RegisterAttachmentCate(New())
}

func New() *sAttachmentCate {
	return &sAttachmentCate{}
}

// GetList 获取附件资源分类列表
func (s *sAttachmentCate) GetList(ctx context.Context, in model.AttachmentCateGetListInput) (out *model.AttachmentCateGetListOutput, err error) {
	out = &model.AttachmentCateGetListOutput{
		Page: in.Page,
		Size: in.Size,
	}
	m := dao.AttachmentCate.Ctx(ctx)
	if in.Title != "" {
		m = m.WhereLike(dao.AttachmentCate.Columns().Title, in.Title)
	}
	if in.Status > 0 {
		m = m.Where(dao.AttachmentCate.Columns().Status, in.Status-1)
	}
	out.Total, err = m.Count()
	if err != nil {
		return out, err
	}
	err = m.Page(in.Page, in.Size).OrderDesc(dao.AttachmentCate.Columns().Id).Scan(&out.List)
	if err != nil {
		return nil, err
	}
	if len(out.List) == 0 {
		return nil, nil
	}
	return out, nil
}

// Create 创建分类
func (s *sAttachmentCate) Create(ctx context.Context, in model.AttachmentCateCreateInput) (out model.AttachmentCateCreateOutput, err error) {
	out = model.AttachmentCateCreateOutput{}
	if in.Pid > 0 {
		var cateModel *entity.AttachmentCate
		err := dao.AttachmentCate.Ctx(ctx).Where(dao.AttachmentCate.Columns().Id, in.Pid).Scan(&cateModel)
		if err != nil {
			return out, err
		}
		in.Level = cateModel.Level + 1
	} else {
		in.Level = 1
	}
	lastInsertID, err := dao.AttachmentCate.Ctx(ctx).Data(in).InsertAndGetId()
	if err != nil {
		return out, err
	}
	out.CateId = uint(lastInsertID)
	return out, err
}

// Update 修改分类
func (s *sAttachmentCate) Update(ctx context.Context, in model.AttachmentCateUpdateInput) error {
	if in.Pid > 0 {
		var cateModel *entity.AttachmentCate
		err := dao.AttachmentCate.Ctx(ctx).Where(dao.AttachmentCate.Columns().Id, in.Pid).Scan(&cateModel)
		if err != nil {
			return err
		}
		in.Level = cateModel.Level + 1
	} else {
		in.Level = 1
	}
	_, err := dao.AttachmentCate.Ctx(ctx).Data(in).FieldsEx(dao.AttachmentCate.Columns().Id).Where(dao.AttachmentCate.Columns().Id, in.Id).Update()
	if err != nil {
		return err
	}
	return nil
}

// GetDetail 获取分类详情
func (s *sAttachmentCate) GetDetail(ctx context.Context, in model.AttachmentCateGetDetailInput) (out *model.AttachmentCateGetDetailOutput, err error) {
	out = &model.AttachmentCateGetDetailOutput{}
	err = dao.AttachmentCate.Ctx(ctx).Where(dao.AttachmentCate.Columns().Id, in.Id).Scan(&out)
	if err != nil {
		return nil, err
	}
	return out, nil
}

// Delete 删除分类
func (s *sAttachmentCate) Delete(ctx context.Context, in model.AttachmentCateGetDetailInput) error {
	_, err := dao.AttachmentCate.Ctx(ctx).Where(dao.AttachmentCate.Columns().Id, in.Id).Unscoped().Delete()
	if err != nil {
		return err
	}
	return nil
}

// GetCateTree 获取菜单树
func (s *sAttachmentCate) GetCateTree(ctx context.Context) ([]*model.CateItem, error) {
	var castes []*model.CateItem
	err := dao.AttachmentCate.Ctx(ctx).Fields(
		dao.AttachmentCate.Columns().Id,
		dao.AttachmentCate.Columns().Title,
		dao.AttachmentCate.Columns().Pid,
		dao.AttachmentCate.Columns().Level).Where(
		g.Map{
			dao.AttachmentCate.Columns().Status: consts.StatusEnable,
		}).Scan(&castes)
	if err != nil {
		return nil, err
	}
	return buildTree(castes, 0), nil
}

// 生成分类数
func buildTree(castes []*model.CateItem, pid uint) []*model.CateItem {
	//定义子节点目录
	var nodes []*model.CateItem
	if reflect.ValueOf(castes).IsValid() {
		//循环所有一级菜单
		for _, v := range castes {
			//查询所有该菜单下的所有子菜单
			if v.Pid == pid {
				//特别注意压入元素不是单个所有加三个 **...** 告诉切片无论多少元素一并压入
				v.Children = append(v.Children, buildTree(castes, v.Id)...)
				nodes = append(nodes, v)
			}
		}
	}
	return nodes
}
